package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description
 * @ClassName TbSpecificationServiceImpl
 * @Author RC
 * @date 2020.12.10 08:50
 */
@Component
@Service
public class TbSpecificationServiceImpl implements TbSpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    @Transactional
    public PageBean<TbSpecification> getSpecificationList(Integer pageNumber, Integer pageSize) {
        PageBean<TbSpecification> page = new PageBean<>();
        page.setPageNumber(pageNumber);
        page.setPageSize(pageSize);
//分页查询
        List<TbSpecification> list= tbSpecificationMapper.selectByPage(page.getStartIndex(),pageSize);
//查询总条数
        int i = tbSpecificationMapper.selectCounts();
        page.setTotalCount(i);
        page.setResult(list);
        return page;
    }

    @Override
    @Transactional
    public void saveSpciOption(String specName, List<TbSpecificationOption> lis) {

        //先增加返回id
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setSpecName(specName);
        int i = tbSpecificationMapper.saveSpciOptionRetrunId(tbSpecification);
        //在 将返回的id和 当前数据一起增加到规格选项当中
        lis.forEach(x -> x.setSpecId(tbSpecification.getId()));
        int i1 = tbSpecificationOptionMapper.insertSpecOptions(lis);
    }


    @Override
    public TbSpecification getBrandById(Long id) {
        return tbSpecificationMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateSpciOption(TbSpecification tbSpecification, List<TbSpecificationOption> specificationList) {

        //修改规格表
        int i = tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //修改分类表
        specificationList.forEach(x -> {
            x.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.updateByPrimaryKeySelective(x); });

    }

    @Override
    public void deleteBatchSpecOption(Integer[] ids) {
        tbSpecificationMapper.deleteBatchSpecOption(ids);
    }

    @Override
    public List<TbSpecification> specificationList() {
        return tbSpecificationMapper.selectByExample(null);
    }
}
